#region Usings

using System.Text;
using System.Xml;
using System.Xml.XPath;

#endregion

namespace Kazkommertsbank.Framework.Model.Pay
{
    public class PaymentMerchant
    {
        //private const string FORMAT = "<merchant cert_id=\"{0}\" name=\"{1}\">{2}</merchant>";

        public PaymentMerchant()
        {}

        public PaymentMerchant(XPathNavigator nav)
        {
            Name = nav.GetAttribute("name", "");
            CertId = nav.GetAttribute("cert_id", "");
            XPathNavigator navigator = nav.SelectSingleNode("order");
            if(navigator != null)
                Order = new PaymentOrder(navigator);
        }

        public string Name { get; set; }
        public string CertId { get; set; }
        public PaymentOrder Order { get; set; }

        public string ToXmlString()
        {
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Encoding = new ASCIIEncoding();
            settings.Indent = false;
            settings.OmitXmlDeclaration = true;

            StringBuilder result = new StringBuilder();
            using(XmlWriter writer = XmlWriter.Create(result, settings))
            {
                writer.WriteStartElement("merchant");
                writer.WriteAttributeString("cert_id", CertId);
                writer.WriteAttributeString("name", Name);
                writer.WriteRaw(Order.ToXmlString());
                writer.WriteEndElement();
            }

            return result.ToString();
            //return string.Format(FORMAT, CertId, Name, Order.ToXmlString());
        }
    }
}